﻿'Programmé par Michel Gagnon
Public Class GestionPersonneRessource
    Dim oLocalContext As DBMainContext
    Dim localPersResc As Pers_Ressource
    Dim ajout As Boolean
    Dim modif As Boolean
    Dim lecture As Boolean
    Dim benef As Beneficiaire

    Sub New(ByVal oBenef As Beneficiaire)

        Me.New(New Pers_Ressource, oBenef)
        btnModifier.Image = My.Resources.Images.disk_blue
        Disponible()
        ajout = True
        lecture = False
    End Sub
    Function validation()
        Dim valide As Boolean = True

        '----vérification sur le nom----
        If txtNom.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtNom, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtNom)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, txtNom.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtNom, My.Resources.MessageErreur.ErreurNom)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtNom)
            End If
        End If

        '----vérification sur le prenom----
        If txtPrenom.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtPrenom, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtPrenom)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, txtPrenom.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtPrenom, My.Resources.MessageErreur.ErreurPrenom)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtPrenom)
            End If
        End If

        '----vérification sur le lien----
        If txtLien.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtLien, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtLien)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, txtLien.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtLien, My.Resources.MessageErreur.ErreurLien)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtLien)
            End If
        End If


        '----vérification sur la priorité----
        If cbPriorite.Text = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, cbPriorite, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbPriorite)
        End If


        '----vérification sur l'adresse----
        If txtAdresse.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtAdresse, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtAdresse)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.adresse, txtAdresse.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtAdresse, My.Resources.MessageErreur.ErreurAdresse)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtAdresse)
            End If
        End If


        '----vérification sur le codePostal----
        If txtCodePostal.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtCodePostal, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtCodePostal)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.CodePostalQc, txtCodePostal.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtCodePostal, My.Resources.MessageErreur.ErreurCodePostal)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtCodePostal)
            End If
        End If


        '----vérification sur la ville----
        If cbVille.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, cbVille, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbVille)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, cbVille.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, cbVille, My.Resources.MessageErreur.ErreurVille)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, cbVille)
            End If
        End If


        '----vérification sur la province----
        If cbProvince.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, cbProvince, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbProvince)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, cbProvince.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, cbProvince, My.Resources.MessageErreur.ErreurProvince)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, cbProvince)
            End If
        End If


        '----vérification sur le pays----
        If cbPays.Text = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, cbPays, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbPays)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, cbPays.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, cbPays, My.Resources.MessageErreur.ErreurPays)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, cbPays)
            End If
        End If


        '----vérification sur les téléphone----

        '----vide----
        If Not (Not txtCell.Text = "(   )    -" Or Not txtMaison.Text = "(   )    -" Or Not txtTravail.Text = "(   )    -") Then
            Outils.ErrorProviderFunctions.AddError(Me, txtCell, My.Resources.MessageErreur.DoitContenirTelephone)
            Outils.ErrorProviderFunctions.AddError(Me, txtMaison, My.Resources.MessageErreur.DoitContenirTelephone)
            Outils.ErrorProviderFunctions.AddError(Me, txtTravail, My.Resources.MessageErreur.DoitContenirTelephone)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtCell)
            Outils.ErrorProviderFunctions.RemoveError(Me, txtMaison)
            Outils.ErrorProviderFunctions.RemoveError(Me, txtTravail)
        End If


        '----Numero de la france----

        If cbPays.Text = "France" Then


            If Not (Not txtCell.Text = "               " Or Not txtMaison.Text = "               " Or Not txtTravail.Text = "               ") Then
                Outils.ErrorProviderFunctions.AddError(Me, txtCell, My.Resources.MessageErreur.DoitContenirTelephone)
                Outils.ErrorProviderFunctions.AddError(Me, txtMaison, My.Resources.MessageErreur.DoitContenirTelephone)
                Outils.ErrorProviderFunctions.AddError(Me, txtTravail, My.Resources.MessageErreur.DoitContenirTelephone)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtCell)
                Outils.ErrorProviderFunctions.RemoveError(Me, txtMaison)
                Outils.ErrorProviderFunctions.RemoveError(Me, txtTravail)

                '----Regex----
                If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.TelFrance, txtCell.Text) And Not txtCell.Text = "               " Then
                    Outils.ErrorProviderFunctions.AddError(Me, txtCell, My.Resources.MessageErreur.FormatTelFrance)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, txtCell)
                End If

                If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.TelFrance, txtMaison.Text) And Not txtMaison.Text = "               " Then
                    Outils.ErrorProviderFunctions.AddError(Me, txtMaison, My.Resources.MessageErreur.FormatTelFrance)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, txtMaison)
                End If

                If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.TelFrance, txtTravail.Text) And Not txtTravail.Text = "               " Then
                    Outils.ErrorProviderFunctions.AddError(Me, txtTravail, My.Resources.MessageErreur.FormatTelFrance)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, txtTravail)
                End If
            End If
        End If


        If cbPays.Text = "Canada" Then

            If Not (Not txtCell.Text = "(   )    -" Or Not txtMaison.Text = "(   )    -" Or Not txtTravail.Text = "(   )    -") Then
                Outils.ErrorProviderFunctions.AddError(Me, txtCell, My.Resources.MessageErreur.DoitContenirTelephone)
                Outils.ErrorProviderFunctions.AddError(Me, txtMaison, My.Resources.MessageErreur.DoitContenirTelephone)
                Outils.ErrorProviderFunctions.AddError(Me, txtTravail, My.Resources.MessageErreur.DoitContenirTelephone)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtCell)
                Outils.ErrorProviderFunctions.RemoveError(Me, txtMaison)
                Outils.ErrorProviderFunctions.RemoveError(Me, txtTravail)

                '----Regex----
                If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.TelQuebec, txtCell.Text) And Not txtCell.Text = "(   )    -" Then
                    Outils.ErrorProviderFunctions.AddError(Me, txtCell, My.Resources.MessageErreur.FormatTelQuebec)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, txtCell)
                End If

                If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.TelQuebec, txtMaison.Text) And Not txtMaison.Text = "(   )    -" Then
                    Outils.ErrorProviderFunctions.AddError(Me, txtMaison, My.Resources.MessageErreur.FormatTelQuebec)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, txtMaison)
                End If

                If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.TelQuebec, txtTravail.Text) And Not txtTravail.Text = "(   )    -" Then
                    Outils.ErrorProviderFunctions.AddError(Me, txtTravail, My.Resources.MessageErreur.FormatTelQuebec)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, txtTravail)
                End If

            End If
        End If

        '----vérification de la validiter de tout les chanps----
        If valide = True Then
            Return True
        Else
            Return False
        End If

    End Function

    Sub Disponible()
        txtNom.Enabled = True
        txtPrenom.Enabled = True
        txtLien.Enabled = True
        cbPriorite.Enabled = True
        txtAdresse.Enabled = True
        txtCodePostal.Enabled = True
        cbVille.Enabled = True
        cbProvince.Enabled = True
        cbPays.Enabled = True
        txtCell.Enabled = True
        txtMaison.Enabled = True
        txtTravail.Enabled = True
    End Sub
    Sub Indisponible()
        txtNom.Enabled = False
        txtPrenom.Enabled = False
        txtLien.Enabled = False
        cbPriorite.Enabled = False
        txtAdresse.Enabled = False
        txtCodePostal.Enabled = False
        cbVille.Enabled = False
        cbProvince.Enabled = False
        cbPays.Enabled = False
        txtCell.Enabled = False
        txtMaison.Enabled = False
        txtTravail.Enabled = False
    End Sub
    Sub New(ByVal oPersResc As Pers_Ressource, ByVal oBenef As Beneficiaire)
        
        InitializeComponent()
        localPersResc = oPersResc
        benef = oBenef
        oLocalContext = New DBMainContext
        initialize()
        lecture = True
        Indisponible()
        'cbVille.DataSource = (From u As Beneficiaire In oLocalContext.Beneficiaire Select u.Ville).ToList() '.AddRange((From v In oLocalContext.Employe Select v.Ville).ToList)
        ' cbVille.DisplayMember = "Ville"
        'cbVille.ValueMember = "Ville"
    End Sub
    Sub initialize()
        '------------------------------------------------------------------------------------------------------si changer en cours il garde lancienne valeur
        cbVille.Load()
        cbProvince.Load()
        cbPays.Load()
        txtNom.Text = localPersResc.Nom
        txtPrenom.Text = localPersResc.Prenom
        txtLien.Text = localPersResc.Lien
        If localPersResc.Priorite Is Nothing Then
            cbPriorite.SelectedText = "1"
        Else
            cbPriorite.Text = localPersResc.Priorite
        End If
        txtAdresse.Text = localPersResc.Adresse
        txtCodePostal.Text = localPersResc.CodePostal
        cbVille.SelectedText = localPersResc.Ville
        cbProvince.SelectedText = localPersResc.Province
        cbPays.SelectedText = localPersResc.Pays

        If cbPays.Text = "France" Then
            txtMaison.Mask = "00 00 00 00 00 00"
            txtCell.Mask = "00 00 00 00 00 00"
            txtTravail.Mask = "00 00 00 00 00 00"
        ElseIf cbPays.Text = "Canada" Then
            txtMaison.Mask = "(000) 000-0000"
            txtCell.Mask = "(000) 000-0000"
            txtTravail.Mask = "(000) 000-0000"
        End If

        txtCell.Text = localPersResc.TelCel
        txtMaison.Text = localPersResc.TelMaison
        txtTravail.Text = localPersResc.TelTravail

    End Sub
    Sub resetChamps()
        txtNom.Text = ""
        txtPrenom.Text = ""
        txtLien.Text = ""
        cbPriorite.SelectedItem = ""
        txtAdresse.Text = ""
        txtCodePostal.Text = ""
        cbVille.SelectedText = ""
        cbProvince.SelectedText = ""
        txtCell.Text = ""
        txtMaison.Text = ""
        txtTravail.Text = ""
        cbPays.SelectedText = ""
    End Sub
    Private Sub GestionPersonneRessource_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifier.Click

        If ajout = True Then

            If validation() = True Then

                With localPersResc
                    .Nom = txtNom.Text.Trim
                    .Prenom = txtPrenom.Text.Trim
                    .Lien = txtLien.Text.Trim
                    .Priorite = cbPriorite.Text
                    .Adresse = txtAdresse.Text.Trim
                    .CodePostal = txtCodePostal.Text.Trim
                    .Ville = cbVille.Text.Trim
                    .Province = cbProvince.Text.Trim
                    .TelCel = txtCell.Text
                    .TelMaison = txtMaison.Text
                    .TelTravail = txtTravail.Text
                    .Pays = cbPays.Text.Trim

                End With
                benef = (From u In oLocalContext.Beneficiaire Where u.ID = benef.ID Select u).FirstOrDefault
                benef.Pers_Ressource.Add(localPersResc)
                oLocalContext.SubmitChanges()
                btnModifier.Image = My.Resources.Images.edit
                Indisponible()
                ajout = False
                lecture = True
            End If

        ElseIf lecture = True Then
            Disponible()
            btnModifier.Image = My.Resources.Images.disk_blue
            lecture = False
            modif = True
            btnSupprimer.Enabled = True
        ElseIf modif = True Then

            If validation() = True Then
                Dim oPersRescModif As Pers_Ressource
                oPersRescModif = (From u In oLocalContext.Pers_Ressource Where u.ID = localPersResc.ID Select u).FirstOrDefault

                With oPersRescModif
                    .Nom = txtNom.Text.Trim
                    .Prenom = txtPrenom.Text.Trim
                    .Lien = txtLien.Text.Trim
                    .Priorite = cbPriorite.Text
                    .Adresse = txtAdresse.Text.Trim
                    .CodePostal = txtCodePostal.Text.Trim
                    .Ville = cbVille.Text.Trim
                    .Province = cbProvince.Text.Trim
                    .TelCel = txtCell.Text
                    .TelMaison = txtMaison.Text
                    .TelTravail = txtTravail.Text
                    .Pays = cbPays.Text.Trim

                End With

                oLocalContext.SubmitChanges()
                modif = False
                Indisponible()
                btnModifier.Image = My.Resources.Images.edit
                lecture = True
            End If

        End If
    End Sub
    Private Sub btnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprimer.Click
        If MsgBox("Êtes-vous sûr de vouloir supprimer cette personne ressource?", vbExclamation + vbYesNo, "Confirmation") = vbYes Then
            localPersResc = (From u In oLocalContext.Pers_Ressource Where u.ID = localPersResc.ID Select u).FirstOrDefault

            btnSupprimer.Enabled = False
            oLocalContext.Pers_Ressource.DeleteOnSubmit(localPersResc)
            oLocalContext.SubmitChanges()
            modif = False
            lecture = True
            CType(Me.Parent, CustomMessageBox).Close()   '

            'btnModifier.Image = My.Resources.Images.disk_blue
        End If
    End Sub
    Private Sub txtNom_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNom.Validated
        txtNom.Text = Outils.Validation.CapFirstChar(txtNom.Text)
    End Sub
    Private Sub cbPays_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbPays.TextChanged
        If cbPays.Text = "France" Then
            txtMaison.Mask = "00 00 00 00 00 00"
            txtCell.Mask = "00 00 00 00 00 00"
            txtTravail.Mask = "00 00 00 00 00 00"
        ElseIf cbPays.Text = "Canada" Then
            txtMaison.Mask = "(000) 000-0000"
            txtCell.Mask = "(000) 000-0000"
            txtTravail.Mask = "(000) 000-0000"
        End If
    End Sub
    Private Sub txtPrenom_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPrenom.Validated
        txtPrenom.Text = Outils.Validation.CapFirstChar(txtPrenom.Text)
    End Sub
    Private Sub txtLien_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLien.Validated
        txtLien.Text = Outils.Validation.CapFirstChar(txtLien.Text)
    End Sub

    Private Sub txtCell_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles txtCell.MaskInputRejected

    End Sub
End Class
